package com.hk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hk.Enume.OperationType;
import com.hk.annotation.AotoFile;
import com.hk.constant.LogConstaant;
import com.hk.core.Result;
import com.hk.domain.SysMenu;
import com.hk.mapper.SysMenuMapper;
import com.hk.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/system/menu")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    private final SysMenuMapper sysMenuMapper;

    /**
     * 查询菜单列表
     * @param menuName 菜单名称
     * @param status 菜单状态
     * @return
     */
    @GetMapping("/list")
    public Result list(String menuName,Integer status){
        log.info("开始查询菜单列表");
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        //支持菜单名称模糊查询菜单
        wrapper.like(StringUtils.isNotEmpty(menuName),SysMenu::getMenuName,menuName);
        //支持状态查询菜单
        wrapper.eq(status != null,SysMenu::getStatus,status);
        List<SysMenu> sysMenus = sysMenuMapper.selectList(wrapper);
        return Result.success(sysMenus);
    }

    /**
     * 添加菜单
     * @param sysMenu
     * @return
     */
    @AotoFile(value = OperationType.POST,title = LogConstaant.MENU_LOG)
    @PostMapping
    public Result addMenu(@RequestBody SysMenu sysMenu){
        log.info("开始添加菜单");
        sysMenuService.save(sysMenu);
        return Result.success();
    }

    /**
     * 修改菜单时根据菜单id获取菜单详情(页面回显)
     * @param menuId
     * @return
     */
    @GetMapping("/{id}")
    public Result getMenuById(@PathVariable("id") Integer menuId){
        SysMenu sysMenu = sysMenuService.getById(menuId);
        return Result.success(sysMenu);
    }

    /**
     * 修改菜单
     * @param sysMenu
     * @return
     */
    @AotoFile(value = OperationType.PUT,title = LogConstaant.MENU_LOG)
    @PutMapping
    public Result updateMenu(@RequestBody SysMenu sysMenu){
        log.info("修改菜单：{}",sysMenu);
        sysMenuService.updateById(sysMenu);
        return Result.success();
    }

    /**
     * 删除菜单
     * @param menuId 菜单id
     * @return
     */
    @AotoFile(value = OperationType.DELETE,title = LogConstaant.MENU_LOG)
    @DeleteMapping("/{id}")
    public Result deleteMenu(@PathVariable("id") Integer menuId){
        log.info("开始删除菜单{}",menuId);
        sysMenuMapper.deleteById(menuId);
        return Result.success();
    }

    /**
     * 获取菜单树形结构数据(下拉框)
     * @return
     */
    @GetMapping("/treeselect")
    public Result getMenuInfo(){
        return Result.success(sysMenuMapper.selectOrgTree());
    }
}
